package org.example;

import org.example.Enum.ActionEnum;
import org.example.Enum.States;
import org.example.Events.*;

import java.util.ArrayList;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        // 这里用控制台输入代表文件输入
        Scanner scanner = new Scanner(System.in);
        String file_string = scanner.nextLine();
        System.out.println("原始Str: " + file_string);
        StateMachine stateMachine = machineConfig();
        ArrayList<String> strings = new ArrayList<>();
        String retString = "";
        for (int i = 0; i < file_string.length(); i++){
            if (stateMachine.getCurrentState() != States.CHECKING){
                if (file_string.charAt(i) != '\"'){
                    stateMachine.execute(ActionEnum.READING_NO_YINHAO, null);
                    continue;
                }
                stateMachine.execute(ActionEnum.READING_YINHAO, null); // 进入checking
            }else{
                if (file_string.charAt(i) != '\"' && file_string.charAt(i) != '\\'){
                    stateMachine.execute(ActionEnum.READING_NO_SPECIFIC_CHAR, null);
                    retString += file_string.charAt(i);
                }else if(file_string.charAt(i) == '\"'){
                    stateMachine.execute(ActionEnum.READING_YINHAO, null);
                    strings.add(retString);
                    retString = "";
                    stateMachine.execute(ActionEnum.READED_NEXT, null);
                }else{
                    stateMachine.execute(ActionEnum.REAING_FANXIEGANG, null);
                    retString += file_string.charAt(i + 1);
                    i += 1;
                    stateMachine.execute(ActionEnum.READED_NEXT, null);
                }
            }

        }
        for (String s : strings) System.out.println(s);
    }

    protected static StateMachine machineConfig(){
        // 初始状态为reading
        StateMachine stateMachine = new StateMachine(States.READING);
        // 初始状态为reading下，读入到不是引号，保持reading
        stateMachine.addTranslation().source(States.READING).when(ActionEnum.READING_NO_YINHAO)
                .Condition(null).targets(States.READING).how(new RemainReading());
        // 初始状态为reading下，读入到引号，进入checking
        stateMachine.addTranslation().source(States.READING).when(ActionEnum.READING_YINHAO)
                .Condition(null).targets(States.CHECKING).how(new EnterStrs());
        // 初始状态为checking下，读入到既不是引号，也不是反斜杠，保持chekcing
        stateMachine.addTranslation().source(States.CHECKING).when(ActionEnum.READING_NO_SPECIFIC_CHAR)
                .Condition(null).targets(States.CHECKING).how(new RemainChecking());
        // 初始状态为checking下，读入到反斜杠，进入转义读入
        stateMachine.addTranslation().source(States.CHECKING).when(ActionEnum.REAING_FANXIEGANG)
                .Condition(null).targets(States.ESCAPE_READING).how(new EscapeReading());
        // 初始状态为EscapeReading下，下一个字符读完，进入checking
        stateMachine.addTranslation().source(States.ESCAPE_READING).when(ActionEnum.READED_NEXT)
                .Condition(null).targets(States.CHECKING).how(new ContiueReading());
        // 初始状态为checking下，读入到引号，结束字符串
        stateMachine.addTranslation().source(States.CHECKING).when(ActionEnum.READING_YINHAO)
                .Condition(null).targets(States.ENDING_READING_CHARS).how(new EndStr());
        //  初始状态为ending_reading下，下一个字符读完，进入reading
        stateMachine.addTranslation().source(States.ENDING_READING_CHARS).when(ActionEnum.READED_NEXT)
                .Condition(null).targets(States.READING).how(new EndStr());
        return stateMachine;
    }
}